package com.batchengine.cron;

import com.batchengine.DataAdapter;
import com.batchengine.Definitions;
import com.batchengine.task.ScheduledJob;
import com.batchengine.task.TaskInfo;
import com.batchengine.task.TaskParser;
import com.batchengine.util.LoggingFactory;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;

import static com.batchengine.Definitions.DataAdapter.FILE;
import static com.batchengine.util.EnvUtil.getConfFolderLocation;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.String.format;

/**
 * Created by  solutions
 * Author: sunil.incubator@yahoo.com
 * Date: Jul 23, 2009
 */
public abstract class ScheduledJobParser {

    private static final Logger logger = LoggingFactory.createLogger();

    private ScheduledJobParser() {
    }

    public static ScheduledJobParser getCronDataParser(Definitions.DataAdapter adapter) {

        if(Definitions.DataAdapter.FILE.equals(adapter)) {
            return new FileScheduledJobParser();
        }
        return new FileScheduledJobParser();
    }

    public abstract Collection<ScheduledJob> loadData();

    /**
     * This class parses configuration file of the format
     * #jobid, taskid, instance-name, workers, expression
     */
    private static class FileScheduledJobParser extends ScheduledJobParser {

        public Collection<ScheduledJob> loadData() {

            Collection<ScheduledJob> scheduledJobs = new ArrayList<ScheduledJob>();

            try {

                String line;
                BufferedReader reader = new BufferedReader(new FileReader(new File(getConfFolderLocation() + "/scheduledjobs.conf")));
                while(null != (line = reader.readLine())) {

                    if(line.startsWith("#") || 0 == line.trim().length()) {
                        continue;
                    }

                    String[] strings = line.split(",");

                    if(5 != strings.length || !line.endsWith(";")) {
                        logger.error(format("#not able to parse task config line %s", line));
                        continue;
                    }

                    ScheduledJob job = new ScheduledJob(parseLong(strings[0]));
                    TaskInfo taskInfo = TaskParser.getTaskDataParser(DataAdapter.FILE).getTaskConfigurationMap().get(parseLong(strings[1]));
                    job.setTaskInfo(taskInfo);
                    job.setInstanceName(strings[2]);
                    job.setMinWorkers(parseInt(strings[3]));
                    job.setMaxWorkers(parseInt(strings[3]));
                    job.setExpression(strings[4].substring(0, strings[4].indexOf(";")));
                    scheduledJobs.add(job);
                }
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
            return scheduledJobs;
        }

    }

    public static void main(String[] args) {

        ScheduledJobParser parser = getCronDataParser(FILE);
        System.out.println(parser.loadData());
    }
}
